import sys
import random
import math
from optparse import OptionParser

class Flow:
    def __init__(self, src, dst, size, t):
        self.src, self.dst, self.size, self.t = src, dst, size, t
    def __str__(self):
        return "%d %d 3 %d %.9f"%(self.src, self.dst, self.size, self.t)

def translate_bandwidth(b):
    if b == None:
        return None
    if type(b)!=str:
        return None
    if b[-1] == 'G':
        return float(b[:-1])*1e9
    if b[-1] == 'M':
        return float(b[:-1])*1e6
    if b[-1] == 'K':
        return float(b[:-1])*1e3
    return float(b)

def poisson(lam):
    return -math.log(1-random.random())*lam

if __name__ == "__main__":

    parser = OptionParser()
    parser.add_option("-f", "--flow_size", dest = "flow_size", help = "original flow size (bytes)", default = "100000")
    parser.add_option("-i", "--split_num", dest = "split_num", help = "split flow to split_num LessFLows", default = "1")
    parser.add_option("-n", "--nhost", dest = "nhost", help = "number of hosts")
    parser.add_option("-o", "--output", dest = "output", help = "the output file", default = "tmp_traffic.txt")
    parser.add_option("-z", "--lessflow_output", dest = "lessflow_output", help = "the lessflow_output file", default = "tmp_traffic.txt")
    options,args = parser.parse_args()

    base_t = 2000000000 # 2000000000

    if not options.nhost:
        print("please use -n to enter number of hosts")
        sys.exit(0)
    nhost = int(options.nhost)
    output = options.output
    flow_size = int(options.flow_size)
    split_num = int(options.split_num)

    ofile = open(output, "w")

    # generate flows
    
    # all2all traffic
    round_num = nhost - 1
    # all_to_all
    mask = 1
    communication_size = flow_size
    for _ in range(0, round_num):
        a_round = []
        # tmp mask test 
        mask = int(nhost / 2)
        for pair in range(0, nhost):
            NIC_src = pair
            NIC_dst = (pair ^ mask)
            a_round.append((NIC_src, NIC_dst))

        # tmp k2flows 
        mask = int(nhost / 2) + 10
        for pair in range(0, nhost):
            NIC_src = pair
            NIC_dst = (pair ^ mask)
            a_round.append((NIC_src, NIC_dst))
        
        break
        # all_to_all_pair_list.append(a_round)
        mask = mask + 1
  
    priority_group = 3
    start_time = 2
 
    if split_num < 2:
        ofile.write("%d \n" % len(a_round))
        for NIC_src, NIC_dst in a_round:
            ofile.write(f"{NIC_src} {NIC_dst} {priority_group} {communication_size} {start_time}\n")
        # ofile.seek(0)
        # ofile.write("%d"%n_flow)
        ofile.close()
    else:
        lessflow_output = options.lessflow_output
        subfile = open(lessflow_output, "w")
        subfile.write("%d \n" % (len(a_round) * split_num))
        communication_size = int(flow_size / split_num)
        for NIC_src, NIC_dst in a_round:
            for pg in range(split_num):
                subfile.write(f"{NIC_src} {NIC_dst} 3 {communication_size} {start_time}\n")
        subfile.close()
